﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp_CustomControl.Controls
{
    /// <summary>
    /// DatetimePicker.xaml 的交互逻辑
    /// </summary>
    public partial class DatetimePicker : UserControl
    {
        public DatetimePicker()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 当前时间
        /// </summary>
        public DateTime CurrentTime
        {
            get { return (DateTime)GetValue(CurrentTimeProperty); }
            set { SetValue(CurrentTimeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentTime.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentTimeProperty =
            DependencyProperty.Register("CurrentTime", typeof(DateTime), typeof(DatetimePicker), new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(OnCurrentTimeChangeEvent)));




        /// <summary>
        /// 当前时间改变
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnCurrentTimeChangeEvent(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DatetimePicker? dp = (DatetimePicker?)d;
            if (dp != null)
            {
                dp.RefreshCurrentTime();
            }
        }

        private void RefreshCurrentTime()
        {
            this.calendar.SelectedDate = CurrentTime;
            this.tb_Hour.Text = CurrentTime.Hour.ToString("D2");
            this.tb_Minute.Text = CurrentTime.Minute.ToString("D2");
            this.tb_Second.Text = CurrentTime.Second.ToString("D2");
        }

        /// <summary>
        /// 确认时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Accept_Click(object sender, RoutedEventArgs e)
        {
            DateTime? selectDate = this.calendar.SelectedDate;
            string _str_Hour = this.tb_Hour.Text.Trim();
            string _str_Minute = this.tb_Minute.Text.Trim();
            string _str_Second = this.tb_Second.Text.Trim();
            int _hour = 0;
            int _minute = 0;
            int _second = 0;
            if (int.TryParse(_str_Hour, out _hour) && int.TryParse(_str_Minute, out _minute) && int.TryParse(_str_Second, out _second) && selectDate != null)
            {
                CurrentTime = new DateTime(selectDate.Value.Year, selectDate.Value.Month, selectDate.Value.Day, _hour, _minute, _second);
                this.popup.IsOpen = false;
            }
        }

        /// <summary>
        /// 小时切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Hour_Click(object sender, RoutedEventArgs e)
        {
            RepeatButton button = (RepeatButton)sender;
            string _Oper = button.Tag.ToString()!;
            string _str_Hour = this.tb_Hour.Text.Trim();
            int _hour = 0;
            if (!int.TryParse(_str_Hour, out _hour))
                return;
            if (_Oper == "Up")
            {
                _hour--;
                if (_hour < 0)
                    _hour = 23;
            }
            else
            {
                _hour++;
                if (_hour > 23)
                    _hour = 0;
            }
            this.tb_Hour.Text = _hour.ToString("D2");
        }

        /// <summary>
        /// 分和秒向上切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Min_Sec_Up_Click(object sender, RoutedEventArgs e)
        {
            RepeatButton repeatButton = (RepeatButton)sender;
            TextBox textBox = (TextBox)repeatButton.Tag;
            string _strValue = textBox.Text.Trim();
            int _value = 0;
            if (!int.TryParse(_strValue, out _value))
                return;
            _value--;
            if (_value < 0)
                _value = 59;
            textBox.Text = _value.ToString("D2");
        }

        /// <summary>
        /// 分秒向下切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Min_Sec_Down_Click(object sender, RoutedEventArgs e)
        {
            RepeatButton repeatButton = (RepeatButton)sender;
            TextBox textBox = (TextBox)repeatButton.Tag;
            string _strValue = textBox.Text.Trim();
            int _value = 0;
            if (!int.TryParse(_strValue, out _value))
                return;
            _value++;
            if (_value > 59)
                _value = 0;
            textBox.Text = _value.ToString("D2");
        }

        private void tb_Hour_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            int _value = 0;
            if (!int.TryParse(textBox.Text.Trim(), out _value))
            {
                textBox.Text = DateTime.Now.Hour.ToString("D2");
                return;
            }
            if (_value < 0 || _value > 23)
                textBox.Text = DateTime.Now.Hour.ToString("D2");
        }

        private void tb_Minute_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            int _value = 0;
            if (!int.TryParse(textBox.Text.Trim(), out _value))
            {
                textBox.Text = DateTime.Now.Minute.ToString("D2");
                return;
            }
            if (_value < 0 || _value > 59)
                textBox.Text = DateTime.Now.Minute.ToString("D2");
        }

        private void tb_Second_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            int _value = 0;
            if (!int.TryParse(textBox.Text.Trim(), out _value))
            {
                textBox.Text = DateTime.Now.Second.ToString("D2");
                return;
            }
            if (_value < 0 || _value > 59)
                textBox.Text = DateTime.Now.Second.ToString("D2");
        }
    }
}
